package dao

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/hashicorp/consul/api"
	"log"
	"net/http"
	"obversatory/model"
)

func GetConsulInst(ctx *gin.Context) {
	config := api.DefaultConfig()
	config.Address = "10.16.4.155:8500"

	client, err := api.NewClient(config)
	if err != nil {
		log.Fatalf("Failed to create Consul client: %v", err)
	}

	catalog := client.Catalog()
	services, _, err := catalog.Service("NodeExporters", "", &api.QueryOptions{})
	if err != nil {
		log.Fatalf("Failed to query services: %v", err)
	}

	if len(services) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "Services not found !",
		})
	} else {
		for _, service := range services {
			sb := model.IDCServerBase{
				Base: model.Base{
					Origin:     service.ServiceMeta["vendor"],
					Company:    service.ServiceMeta["account"],
					Department: service.ServiceMeta["group"],
					Project:    service.ServiceMeta["group"],
					Env:        service.ServiceMeta["env"],
					Name:       service.ServiceMeta["name"],
					Instance:   service.ServiceMeta["instance"],
					Tags:       []string{"linux", "vm"},
				},
				Os: service.ServiceMeta["os"],
			}
			fmt.Printf("格式化值为：%#v", sb)
		}
	}
}
